// 21. 合并两个有序链表
// https://leetcode.cn/problems/merge-two-sorted-lists/

/**
示例 1：


输入：l1 = [1,2,4], l2 = [1,3,4]
输出：[1,1,2,3,4,4]
示例 2：

输入：l1 = [], l2 = []
输出：[]
示例 3：

输入：l1 = [], l2 = [0]
输出：[0]
 */

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function (list1, list2) {
    if (list1 === null || list2 === null) {
        return list1 ? list1 : list2 ? list2 : null;
    }

    let firstNode = null;
    if (list1.val < list2.val) {
        firstNode = list1;
        list1.next = mergeTwoLists(list1.next, list2);
    } else {
        firstNode = list2;
        list2.next = mergeTwoLists(list1, list2.next);
    }

    return firstNode;
};
